package com.cunjin.sanitation.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cunjin.base.service.impl.BaseServiceImpl;
import com.cunjin.sanitation.dao.SanitationDao;
import com.cunjin.sanitation.entity.SanitationEntity;
import com.cunjin.sanitation.service.SanitationService;


@Service
public class SanitationServiceImpl extends BaseServiceImpl<SanitationEntity> implements SanitationService {

	@Autowired
	private SanitationDao sanitaDao;
	
	
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> queryByPage(int page, int rows, String searchName) {
		
		//将分页的数据保存在map中
		Map<String, Object> pages=new HashMap<String, Object>();
		
		int before=(page-1)*rows;
		int after=rows;
		pages.put("before", before);
		pages.put("after", after);
		if(null !=searchName){
			pages.put("searchName", searchName+"%");
		}else{
			pages.put("searchName", null);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<SanitationEntity> ls=sanitaDao.myqueryByPage(pages);
			map.put("total",sanitaDao.findAllByName(pages));
			map.put("rows",ls);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 插入
	 */
	@Override
	public Map<String, Object> insert(SanitationEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			//先判断是否重复
			String newtime = entity.getTime();
			String newDormitoryName = entity.getDormitoryName();
			Map<String, Object> condition=new HashMap<String, Object>();
			condition.put("time", newtime);
			condition.put("dormitoryName", newDormitoryName);
			SanitationEntity newentity = sanitaDao.selectByNameAndTime(condition);
			if(null ==newentity){
				int i = sanitaDao.insertSelective(entity);
				if(i==1){
						map.put("code", 200);
						map.put("msg","添加班级成功");
				}else{
						map.put("code", 500);
					}
			}
			if(null !=newentity){
				map.put("code", 201);
				map.put("msg","记录已经存在已经存在");
				}
			
			
		} catch (Exception e) {
			map.put("code", 500);
			e.printStackTrace();
		}
		return map;
	}

	
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> deleteByids(String[] split) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			
			int cs=0;
			for (String s : split) {
				int did = Integer.parseInt(s);
				sanitaDao.deleteByPrimaryKey(did);
				cs++;
			}
			map.put("code", 200);
			map.put("count", cs);
		} catch (Exception e) {
			map.put("code",500);
			e.printStackTrace();
		}
		return map;
	}
	
	@Override
	public Map<String, Object> update(SanitationEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		try {
			//先判断是否重复
			String newtime = entity.getTime();
			String newDormitoryName = entity.getDormitoryName();
			Map<String, Object> condition=new HashMap<String, Object>();
			condition.put("time", newtime);
			condition.put("dormitoryName", newDormitoryName);
			SanitationEntity newentity = sanitaDao.selectByNameAndTime(condition);
			if(null !=newentity){
				int i = sanitaDao.updateByPrimaryKeySelective(entity);
						
				if(i==1){
						map.put("code", 200);
						map.put("msg","添加班级成功");
				}else{
						map.put("code", 500);
					}
			}
			if(null ==newentity){
				map.put("code", 201);
				map.put("msg","记录不存在，请先录入");
				}
			
			
		} catch (Exception e) {
			map.put("code", 500);
			e.printStackTrace();
		}
		return map;
	}


}
